/*******************************************************************/
/*                                                                 */
/*      StartUp.                                                   */
/*                                                                 */
/*      98-05-27 8:21                                              */
/*                                                                 */
/*      Initializatioin                                            */
/*                                                                 */
/*******************************************************************/

#include "../includes.h"


STARTUP_  startup;

void    Init_STARTUP(void);
void    RunSelftestPassLED(void);
void    StartUpProcedure(void);
int     StartUpSet(void);
int		StartUpSet2(void);
int     StartUpSelfTest(void);

int		Setting_LOG(void);
int     Init_SELFTEST(void);

char 	CompleteTestRomRamFlag;

void Init_STARTUP(void)
{

    /* STARTUP data structure initialization */
    startup.MAINSTEP    	= STARTUP_START;
    startup.TEST_RESULT 	= 0;

	startup.SELFTEST		= NO;
	startup.ROM_TEST		= UNDEFINED;

	startup.self_test_step	= self_test_OSC;

	startup.IntervalCounter = 0;
	
	CompleteTestRomRamFlag = NO;
	
}

/*kang
void RunSelftestPassLED(void)
{
	LED_ALL_OFF();

    LED_ALL_OFF();    LED_0_ON();		//wait(200);
    LED_ALL_OFF();    LED_1_ON();		//wait(200);
    LED_ALL_OFF();    LED_2_ON();		//wait(200);
    LED_ALL_OFF();    LED_3_ON();		//wait(200);
    LED_ALL_OFF();    LED_4_ON();		//wait(200);
    LED_ALL_OFF();    LED_5_ON();		//wait(200);
    LED_ALL_OFF();    LED_6_ON();		//wait(200);

    LED_ALL_OFF();
}
*/


/* ---------------------------------------------------------------------
Last Updated  : Tue  08-12-97 18:15:26
Function Name : StartUpProcedure
Description   : SYSTEM initialization
                E/B selftest -> LED -> TOD ->  LeapSecond set
--------------------------------------------------------------------- */
void StartUpProcedure()
{


	//if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){
	//	sprintf((char*)MESG,"S... %d %d %d\r\n",
	//		SYSTEM.OPERATION.OPMODE,startup.IntervalCounter,startup.MAINSTEP);
	//	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
    //
	//}
	
	//printf("StartUpProcedure()->revision\n");

    switch ( startup.MAINSTEP ) {

        case    STARTUP_START:
        			/* hmlee 08-12-01
        			if( startup.IntervalCounter++ >= 1){

	                    if( Setting_LOG() == OK ){
							startup.MAINSTEP = STARTUP_SELFTEST;
							startup.IntervalCounter = 0;	
						}
					}
					*/
					
					/* hmlee 08-12-01 */
					startup.MAINSTEP = STARTUP_SELFTEST;
					startup.IntervalCounter = 0;
       				break;

        case    STARTUP_SELFTEST:
        			
					// hmlee 08-12-01
                    if( StartUpSelfTest() == OK ){
					
						//printf("STARTUP_SELFTEST--->1\n");
						
                        SYSTEM.OPERATION.SELFTEST = startup.TEST_RESULT;

                        if(startup.TEST_RESULT == 0x00){
							
							//printf("STARTUP_SELFTEST--->2\n");

                            //RunSelftestPassLED();
                            startup.SELFTEST = OK;
                            startup.MAINSTEP = STARTUP_SET;
                            
                            Setting_LOG_Flag = ON;
		                    //PrintMessage("\r\nSelf test passed!!\r\n");
                        } 
						else{
								
							//printf("STARTUP_SELFTEST--->3\n");
							
                             startup.TEST_RESULT = 0x00;
		                    //PrintMessage("\r\nRetry Self test..\r\n");
                        }
                        
                    }
					
					
					/* hmlee: temperature, current ID, 08-12-01 */
					//startup.SELFTEST = OK;
                    //startup.MAINSTEP = STARTUP_SET;
					//Setting_LOG_Flag = ON;
					
                    break;

        case    STARTUP_SET:
        			/* hmlee: wait for TEMPREATURE.CURRENT */
					
        			if(Update_ModuleWarmupTime_flag == ON){
						Update_ModuleWarmupTime_flag = OFF;
						Init_Temperature = TEMPERATURE.CURRENT;
						//Update_ModuleWarmupTime(Init_Temperature);
					}

                    if( StartUpSet2() == OK  ) {
	                    startup.MAINSTEP = STARTUP_WARM_START;
	                    startup.IntervalCounter = 0;
                    }

					break;

        case    STARTUP_WARM_START:

//					if( GC.ACCsecond > 0){
						WarmStart(WARM_START_APPLY);

        	           	SYSTEM.OPERATION.OPMODE = OPMODE_WARMUP;
						LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "WarmUp started");
						Config_Control();
//					}

					if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){
						sprintf((char*)MESG,"%d\r\n",GC.ACCsecond);
						TransmitMessage((BYTE*)MESG,PORT_CONTROL);
					}
					break;
		default: 
					break;
		
		break;
     }
}



int StartUpSelfTest()
{

    /* CPU, RAM, ROM, FPGA, OCXO, E/B ( plus ALMANAC ) */
    /* Run LEDs to indicate passing StartUpSelfTest */

	unsigned short s1,s2;
	unsigned short *S_ADDR;


	startup.IntervalCounter++;
	switch(startup.self_test_step){


/*kang
		case self_test_ROM:
                startup.ROM_TEST = OK ;
                startup.self_test_step = self_test_RAM;
                //Skip
		        break;

		case self_test_RAM: 
				startup.self_test_step = self_test_OSC;
				break;
kang*/

		case self_test_OSC:

				CompleteTestRomRamFlag = YES;
				startup.self_test_step = self_test_EB;
				Setting_OCXOID();
				break;


		case self_test_EB:
				//EB_Task_start();
				startup.self_test_step = self_test_LED;
				startup.IntervalCounter = 0;
				//LED_ALL_OFF();
				break;

		case self_test_LED:


				if(startup.IntervalCounter == 1 || startup.IntervalCounter == 2){
					//RunSelftestPassLED();
				}
				else if( startup.IntervalCounter == 3 || startup.IntervalCounter == 5){
					//LED_ALL_ON();
				}
				else if( startup.IntervalCounter == 4 || startup.IntervalCounter == 6){
					//LED_ALL_OFF();
				}

				if(startup.IntervalCounter == 6){
					startup.IntervalCounter = 0;
					return OK;
				}
				break;

         default:

         		break;
	}

    return NO;
}



/* ---------------------------------------------------------------------
Last Updated  : 2002-01-21 7:39��
Function Name : StartUpSet
Description   :
--------------------------------------------------------------------- */

extern int iIRQ0;
int StartUpSet()
{

	//if( SYSTEM.EB.STATUS != EB_STATUS_NONE && GC.RunningTime <= 2*60){

		if( SYSTEM.EB.STATUS == EB_STATUS_RUN ){
			if(SYSTEM.TIME.Valid){
				//MessageOut("GPS Time\r\n");
				//SetRTCAll(SYSTEM.TIME.GPS);
				//LogIt(LOG_Start,GC.RunningTime);
				//LogIt3(LOG_Start,GC.RunningTime, (int)CONFIG.DAC, FirmWare_VersionDef);
				return OK;
			}
		}

		if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){
			sprintf((char*)MESG,
					"Waiting Time... %d,%d)%d %d %d %d %d %d "
					">>"
					"<mat:%d,mis:%d> eb,mid,gps)hour:%2d,%2d,%2d, min:%2d,%2d,%2d, sec:%2d,%2d,%2d"
					"\r\n"
				,SYSTEM.EB.STATUS,SYSTEM.TIME.Valid
				,SYSTEM.OPERATION.LOCKMODE,RECOVERY.flag
				,CHECKLOCK.HoldoverDelayCounter,TOD.SKIP_TIMER,TOD.match_counter,CHECKLOCK.LockedCounter

					,SYSTEM.TIME.TOD_MatchCounter
					,SYSTEM.TIME.TOD_MismatchCounter

					,SYSTEM.EB.TIME.HOUR   ,SYSTEM.TIME.MID.HOUR   ,SYSTEM.TIME.GPS.HOUR
					,SYSTEM.EB.TIME.MINUTE ,SYSTEM.TIME.MID.MINUTE ,SYSTEM.TIME.GPS.MINUTE
					,SYSTEM.EB.TIME.SECOND ,SYSTEM.TIME.MID.SECOND ,SYSTEM.TIME.GPS.SECOND
			);
			TransmitMessage((BYTE*)MESG,PORT_CONTROL);
		}

	return FAIL;
}


int StartUpSet2(void)
{

	//if( SYSTEM.EB.STATUS != EB_STATUS_NONE && GC.RunningTime <= 2*60){
		if( (SYSTEM.EB.STATUS > EB_STATUS_SELFTEST) && (SYSTEM.EB.almanac_step > EB_SET_EB_ID)){
			//LogIt3(LOG_CATA1_NORM, LOG_CATA2_UNSORTED, LOG_Start,GC.RunningTime, (int)CONFIG.DAC, FirmWare_VersionDef);
			return OK;
		}

		if (SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){
			sprintf((char*)MESG,"Waiting GPS... %d %d %d %d %d %d \r\n",
				SYSTEM.OPERATION.LOCKMODE,RECOVERY.flag,
				CHECKLOCK.HoldoverDelayCounter,TOD.SKIP_TIMER,TOD.match_counter,CHECKLOCK.LockedCounter);
			TransmitMessage((BYTE*)MESG,PORT_CONTROL);
		}



	return FAIL;
}

